In [1]:
import pandas as pd
import numpy as np
In [2]:
#First initialize the series by calling the pd.Series() function
randomNumbers = pd.Series( np.random.randint(1, 100, 100) )
#Display the first 5 random numbers
print( randomNumbers.head() )
#Next filter out the odd numbers by using the mod operator and reset the index
evenRandomNumbers = randomNumbers[ randomNumbers % 2 == 0].reset_index( drop=True )
#Display the first 5
evenRandomNumbers.head()
Out[2]:
In [3]:
numbers = ['(342)123-2345', '410-342-3421', '(234 434-2121', '(301)822-3423', '123-234-3423', '(410)555-4443', 'AAAAHHH', '(XXX)XXX-XXXX', '(602)123-4535', '(234)127-4534']
In [4]:
#Predefined list of numbers
numbers = ['(342)123-2345', '410-342-3421', '(234 434-2121', '(301)822-3423', '123-234-3423', '(410)555-4443', 'AAAAHHH', '(XXX)XXX-XXXX', '(602)123-4535', '(234)127-4534']
#Create the phone numbers series
phoneNumbers = pd.Series( numbers )
#Next filter the phone numbers by using the str.match function
validPhoneNumbers = phoneNumbers[ phoneNumbers.str.match( r'\(\d{3}\)\d{3}-\d{4}') ].reset_index( drop=True )
In [5]:
#This function converts a number from Farenheit to Celsius
toCelsius = lambda x: (float(5)/9)*(x-32)
#Creates a series with numbers that represent temperatures in Farenheit
tempsInFarenheit = pd.Series( [92,33,-5,17,122,87 ])
In [6]:
#Your code here...
tempsinCelsius = tempsInFarenheit.apply( toCelsius )
print( tempsinCelsius)
In [7]:
numList = [1,1,1,1,1,2,4,5,7,5,4,5,6,4,3,5,5,5,6,9,0,7,6,7,5,4,4,7]
In [8]:
#Your code here...
numSeries = pd.Series( numList)
numSeries.value_counts()
Out[8]:
You are given a Series of IP Addresses and the goal is to limit this data to private IP addresses. Python has an ipaddress module which provides the capability to create, manipulate and operate on IPv4 and IPv6 addresses and networks. Complete documentation is available here: https://docs.python.org/3/library/ipaddress.html.
Here are some examples of how you might use this module:
import ipaddress
myIP = ipaddress.ip_address( '192.168.0.1' )
myNetwork = ipaddress.ip_network( '192.168.0.0/28' )
#Check membership in network
if myIP in myNetwork: #This works
print "Yay!"
#Loop through CIDR blocks
for ip in myNetwork:
print( ip )
192.168.0.0
192.168.0.1
…
…
192.168.0.13
192.168.0.14
192.168.0.15
#Testing to see if an IP is private
if myIP.is_private:
print( "This IP is private" )
else:
print( "Routable IP" )
ipaddress module.
In [9]:
import ipaddress
hosts = [ '192.168.1.2', '10.10.10.2', '172.143.23.34', '34.34.35.34', '172.15.0.1', '172.17.0.1']
In [10]:
from ipaddress import ip_address
IPData = pd.Series( hosts )
privateIPs = IPData[IPData.apply( lambda x : ip_address(x).is_private ) ]
print( privateIPs )